﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace CNDPKlient
{
    public partial class FormInput : Form
    {
        public string ipAddress;
        public int portNumber;
        public FormInput()
        {
            InitializeComponent();
        }

        private void buttonConnect_Click(object sender, EventArgs e)
        {
            ipAddress = textBoxIpAddress.Text;
            if (textBoxPortNumber.Text == "" || textBoxIpAddress.Text == "")
            {
                labelError.Text = "Fyll i rutorna.";
                labelError.Show();
            }
            else
            {
                //130.243.104.98
                 using (UdpClient c = new UdpClient(9058))
                 {
                     try
                     {
                         portNumber = Convert.ToInt16(textBoxPortNumber.Text);
                         var TestBytes = Encoding.ASCII.GetBytes("TEST"); 
                         c.Send(TestBytes,TestBytes.Length , ipAddress, portNumber);
                         labelError.Text = "Ansluter...";
                         labelError.Show();
                         var asyncresult = c.BeginReceive(null, null);
                         asyncresult.AsyncWaitHandle.WaitOne(TimeSpan.FromSeconds(1));
                         if (asyncresult.IsCompleted)
                         {
                             labelError.ForeColor = System.Drawing.Color.Green;
                             labelError.Text = "Anslutningen lyckades";
                             DialogResult = DialogResult.OK;
                             labelError.ForeColor = System.Drawing.Color.Red;

                         }
                         else
                         {
                             labelError.Text = "Anslutningen misslyckades";
                         }
                     }
                     catch (OverflowException)
                     {
                         labelError.Text = "Porten eller ipaddressen är för långa/stora.";
                         labelError.Show();
                     }
                     catch (FormatException)
                     {
                         labelError.Text = "Porten eller ipaddressen är ej \ni korrekt form.";
                         labelError.Show();
                     }
                     catch (ObjectDisposedException)
                     {
                         labelError.Text = "Servern eller klienten är avstängd.";
                         labelError.Show();
                     }
                     catch (SocketException)
                     {
                         labelError.Text = "Porten är inte öppen.";
                         labelError.Show();
                     }
                     catch (ArgumentNullException)
                     {
                         labelError.Text = "Ipaddressen eller porten är tom.";
                         labelError.Show();
                     }
                     catch (Exception en)
                     {
                         labelError.Text = en.Message + ". Källa" + en.Source;
                         labelError.Show();
                     }
                 }
                 
  
            }
            
        }

        private void FormInput_Load(object sender, EventArgs e)
        {
            labelError.ForeColor = System.Drawing.Color.Red;
            labelError.Hide();

        }

        private void buttonSaveToFile_Click(object sender, EventArgs e)
        {
            try
            {
                if (!Directory.Exists("CNDP"))
                {
                   Directory.CreateDirectory("CNDP");
                }
                System.IO.File.WriteAllText("CNDP/ip.txt", textBoxIpAddress.Text + "|" + textBoxPortNumber.Text);
                labelError.Text = "Ip adressen sparades.";
            }
            catch (ArgumentException ae)
            {
                labelError.Show();
                labelError.Text = ae.Message + " " + ae.Source;
            }
            catch (Exception en)
            {
                labelError.Show();
                labelError.Text ="Error: " + en.Source + " " + en.Message;
            }

        }

        private void buttonGetIP_Click(object sender, EventArgs e)
        {
            try
            {
                
                string text = System.IO.File.ReadAllText("CNDP/ip.txt");
                string[] words = text.Split('|');
                textBoxIpAddress.Text = words[0];
                textBoxPortNumber.Text = words[1];

            }
            catch (DirectoryNotFoundException)
            {
                labelError.Show();
                labelError.Text = "Ingen ip-address finns sparad.";
            }
            catch(FileNotFoundException)
            {
                labelError.Show();
                labelError.Text = "Ingen ip-address finns sparad.";
            }
            catch (Exception er)
            {
                labelError.Show();
                labelError.Text = er.Source + " " + er.Message;
            }
  
        }
       
    }
}
